package com.dream.inspect.modules.business.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dream.inspect.common.utils.DateUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.utils.StringUtil;
import com.dream.inspect.modules.business.dao.SuperviseStandardDao;
import com.dream.inspect.modules.business.dao.SuperviseTempletConfDao;
import com.dream.inspect.modules.dream.dao.SysUserMapper;
import com.dream.inspect.modules.dream.pojo.SysUserFormMap;
import com.dream.inspect.modules.sys.controller.AbstractController;
import com.dream.inspect.modules.sys.dao.SysDeptDao;
import com.dream.inspect.modules.sys.entity.SysDeptEntity;
import com.dream.inspect.modules.sys.entity.SysUserEntity;
import com.dream.inspect.modules.sys.service.SysUserService;


@Controller
@CrossOrigin
@RequestMapping("business/supervise")
public class SuperviseController extends AbstractController {

    @Autowired
    SysDeptDao sysDeptDao;
    @Autowired
    SysUserMapper sysUserDao;
    @Autowired
    SuperviseTempletConfDao superviseTempletConfDao;
    @Autowired
    SuperviseStandardDao superviseStandardDao;
    @Autowired
    SysUserService sysUserService;

    /**
     * 新增督察记录
     * @param id
     * @return
     */
    @RequestMapping("/addRecord")
    public String addRecord(Model model , String id){
        model.addAttribute("dquser", getSessionUser());
        model.addAttribute("bdcdw", getbdcdw(""));
        return "modules/business/record/addDcrw";
    }

    @RequestMapping("/recordDetails")
    public String recordDetails(String id, int type, Model model){
    	model.addAttribute("id", id);
    	model.addAttribute("roleId", getSessionUser().getStr("roleId"));
    	model.addAttribute("dqDwId", getDeptId());
        switch(type){
            case 1:
                return "modules/business/record/supervisionRecordDetails";
            case 2:
                return "modules/business/record/problemTrackingDetails";
            case 3:
                return "modules/business/record/problemModifyDetails";
        }
        return "";
    }
    
    @RequestMapping("/questionOpen")
    public String questionOpen(String questionId, String recordId, String questionModifyId, String questionDescription, int type, Model model){
    	model.addAttribute("questionId", questionId);
    	model.addAttribute("recordId", recordId);
    	model.addAttribute("questionModifyId", questionModifyId);
    	model.addAttribute("questionDescription", questionDescription);
        switch(type){
            case 1:
                return "modules/business/record/auditIssues";
            case 2:
                return "modules/business/record/reviewAndRectify";
        }
        return "";
    }

    @RequestMapping("/openVideo")
    public String openVideo(String url, Model model){
    	model.addAttribute("url", url);
    	
    	return "modules/business/record/videoplayer";
    }

    /**
     * 查询被督查部门接口
     * @param name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getbdcdw")
    public List<Map<String,Object>> getbdcdw(String name) {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name",name);
        map.put("deptId",getDeptId());
        map.put("currPage",0);
        map.put("pageSize",0);
        map.put("sfdctx","1");//是否督查条线(0：是，1：否)，被督查单位只能是非督查条线
        list = sysDeptDao.queryDeptList(map);
        return list;
    }

    /**
     *查询督查人员
     * @param name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getSupervisePersons")
    public List<SysUserFormMap> getSupervisePersons(String name,String flag,String deptId) {
    	if (StringUtil.isEmpty(deptId)) {
    		deptId = String.valueOf(getDeptId());
    	}
        return sysUserDao.queryUserListByPage(deptId,name,0,0,flag);
    }

    /**
     * 查询检查项
     * @param num 督察方式
     * @param templetId 模板Id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCheckList")
    public R getCheckList(String num, String templetId){
        if(StringUtil.isNotEmpty(num) && StringUtil.isEmpty(templetId)){
            SysDeptEntity dept = sysDeptDao.selectById(getDeptId());
            String sfdctx = dept.getSfdctx();
            String deptId="";
            if("1".equals(sfdctx)){
                deptId = dept.getParentId() + "";
            }
            String qhId = dept.getQhId() + "";
            //获取对应标准模板ID
             templetId = String.valueOf(superviseTempletConfDao.queryTempletIdByRQ(String.valueOf(getUserId()),deptId,qhId,num));
            if(StringUtil.isEmpty(templetId)){
                templetId = num;
            }
        }

        //查询检查项
        List<Map<String,Object>> list = superviseStandardDao.queryCheckListByEjCode(templetId, "", 4);
        return R.ok().put("data",list).put("templetId",templetId);
    }
    @ResponseBody
    @RequestMapping("/getUserInfomation") 
    public R getUserInfo () {
    	SysUserEntity user = getUser();
    	SysUserFormMap sysMap = getSessionUser();
    	Long userId = user.getUserId();
    	user.setDeptName(sysMap.getStr("deptName"));
    	user.setRoleName(sysMap.getStr("roleName"));
    	user.setCurrDate(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    	return R.ok().put("userInfo",user).put("userId", userId);
    }

}
